package com.example.outputsql;

import lombok.SneakyThrows;
import lombok.val;
import lombok.var;
import org.junit.Test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.nio.channels.FileChannel;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * @ClassName: leetcode
 * @Description:
 * @Author: csp
 * @Date:
 */
public class OutputFile {
    //列
    String serialNumber = "2020092882563579";
    String enterprise_code = "7829373929";
    String address_telephone = "天津是市区132456749687989745";
    String bank_account = "7894560000000000";
    String create_date = "2020-11-19 10:00:00";
    String gfkhyx = "2090@qq.com";
    String gfkhzh = "13612155859";
    String[] column = {"id", "serial_number", "user_name", "enterprise_code", "enterprise_name", "address_telephone", "bank_account", "pinyin_simple_code", "create_date", "gfkhyx", "gfkhdh"};
    String table = "my_customer_info";

    @Test
   public void test(){
        StringBuffer sbf = new StringBuffer();
        String collect = Arrays.stream(column).collect(Collectors.joining(",", "(", ")"));
        sbf.append("INSERT INTO").append("\n")
                .append(table).append(" ").append(collect).append(" ")
                .append("VALUES").append("\n");
        System.out.println(sbf.toString());
        String path = getClass().getResource("/outputfile/enterprisename.txt").getPath();
        File file = new File(path);
        String[] enterprise_name = txtRead(file);
        for (int i = 0; i < 200000; i++) {
            int k = i % enterprise_name.length;
            String columns = "('%s','2020092882563579','','7829373929','%s','天津是市区132456749687989745','7890000000000000','','2020-11-19 10:00:00','2090@qq.com','13612155859')";
            String format = String.format(columns, UUID.randomUUID(), enterprise_name[k]);
            sbf.append(format).append(",").append(System.lineSeparator());
        }
        String result = sbf.toString().replaceAll(",$", ";");
        outputfile(result);

   }
   private String[] txtRead(File file) {
       List<String> list = new ArrayList<>();
       try {
           BufferedReader br = new BufferedReader(new FileReader(file));//构造一个BufferedReader类来读取文件
//           FileReader reader = new FileReader(file, true);
           String s = null;
           while ((s = br.readLine()) != null) {//使用readLine方法，一次读一行
               list.add(s);
           }
           br.close();
       } catch (Exception e) {
           e.printStackTrace();
       }
       return list.toArray(new String[]{});
   }

    private void outputfile(String str) {
        try {
            OutputStream out = new FileOutputStream("E:/test.sql", true);
            OutputStreamWriter writer = new OutputStreamWriter(out);
//            FileWriter writer = new FileWriter("E:/test.sql", true);
            writer.write(str);
            writer.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @SneakyThrows
    private void mmap() {
        File file = new File("C:\\");
        FileOutputStream out = new FileOutputStream(file);

        Files.copy(Paths.get(""), out);
        FileChannel fileChannel = new FileInputStream(file).getChannel();

        var hashMap = new HashMap<>();
        val objects = new ArrayList<>();
        hashMap.forEach((a, b) -> System.out.println(a));
        objects.forEach(System.out::println);

    }

}
